package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class demo31 {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> queue = new LinkedBlockingQueue<>(1000);
        Thread producer = new Thread(() ->{
            int count = 0;
            try{
                while (true) {
                    queue.put("" + count);
                    System.out.println("生产了一个元素" + count);
                    count++;
                    Thread.sleep(1000);
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        });
        Thread consumer = new Thread(() ->{
            try{
                while (true) {
                    String elem = queue.take();
                    System.out.println("消费了一个元素" + elem);
                    Thread.sleep(1000);
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        });

        producer.start();
        consumer.start();
        producer.join();
        consumer.join();
    }
}
